package com.ideal.timer;

import com.barco.ledApi.BcmCDev;
import com.barco.ledApi.LedConfig;
import com.ideal.Interface.MainConfig;
import com.ideal.log.Log;
import com.sensor.Broadcast;
import java.util.List;
import java.util.Map;
import java.util.Timer;//定时器类
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SysContextListener implements ServletContextListener {

    private Timer getLedInfoTimer = null;
    private Timer getSensorTimer = null;
    private String Curdir = null;
    private String Confdir = null;
    
    private final String ProKey = "projectorList";
    private final String SensorKey= "sensorList";

    public void contextInitialized(ServletContextEvent event) {
        Curdir = System.getProperty("user.dir");//当前路径，为tomcat启动执行路径
        Confdir = Curdir + "/conf/";//配置文件路径

        Log.init(Confdir);//初始化log4j
        Log.logger.info("Init MainConfig");
        MainConfig.init(Confdir);//初始化主配置文件
        Log.logger.info("Init LedConfig");
        LedConfig.init(Confdir);//初始化led配置文件
        //在这里初始化监听器，在tomcat启动的时候监听器启动，可以在这里实现定时器功能

//        Log.logger.info("Create get led info timer");
//        getLedInfoTimer = new Timer("GetBcmcInfo", true);
//        Log.logger.info("Add getBcmc info timer task");
//        BcmCDev bcmc = new BcmCDev();//bcmc接口
//        GetInfoTimer timebean = new GetInfoTimer(bcmc);//获取led信息任务
//        getLedInfoTimer.schedule(timebean, MainConfig.m_LedInfodelay * 1000, MainConfig.m_LedInfoinertval * 1000);//调用exportHistoryBean，0表示任务无延迟，5*1000表示每隔5秒执行任务，60*60*1000表示一个小时。
       
        
        
        
        
        Log.logger.info("Create get sensor info timer");
        getSensorTimer = new Timer("GetSensorInfo", true);
        Log.logger.info("Add sensor info timer task");
        int udp_port = 9000;
        Broadcast sensor = new Broadcast(udp_port);
        sensor.AddNewSensor("192.168.1.54", 10050);
        GetInfoTimer sensorbean = new GetInfoTimer(sensor,SensorKey);//获取led信息任务
        getSensorTimer.schedule(sensorbean, MainConfig.m_LedInfodelay * 1000, MainConfig.m_LedInfoinertval * 1000);

        Log.logger.info("add timer finish");
    }

    public void contextDestroyed(ServletContextEvent event) {//在这里关闭监听器，所以在这里销毁定时器。
        if (getLedInfoTimer != null) {
            getLedInfoTimer.cancel();
        }
        Log.logger.info("timer desctroy");
    }
}
